unit UThemeEdit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UThemeAddEditBase, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinOffice2016Colorful, Vcl.Menus, System.ImageList,
  Vcl.ImgList, cxImageList, Vcl.StdCtrls, cxButtons, Vcl.ExtCtrls, cxMaskEdit,
  cxSpinEdit, cxTextEdit, cxLabel, Data.DB, MemDS, DBAccess, Uni;

type
  TfrmThemeEdit = class(TfrmThemeAddEditBase)
    qryTmp: TUniQuery;
    procedure FormShow(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmThemeEdit: TfrmThemeEdit;

implementation

uses
  UCommonFunSqlite, UCommonConst, UDM;

{$R *.dfm}

procedure TfrmThemeEdit.btnSaveClick(Sender: TObject);
var
  sSql, sName: string;
  iDuration: Integer;
begin
  inherited;

  sName := Trim(edtName.Text);
  if sName = '' then
  begin
    MsgBox(RESTHEMENAMEEMPTY);
    edtName.SetFocus;
    Abort;
  end;
  if Length(sName) > 100 then
  begin
    MsgBox(Format(RESCONTENTLENGTHLIMIT, [100]));
    edtName.SetFocus;
    Abort;
  end;

  iDuration := edtDuration.Value;

  sSql := 'select ThemeName from ThemeData where ThemeName=' + QuotedStr(sName) + ' and ThemeID<>' + QuotedStr(IntToStr(FSelectID));

  if getSingleValueStr(sSql) <> '' then
  begin
    MsgBox(RESTHEMEEXIST);
    edtName.SetFocus;
    Abort;
  end;

  sSql := 'update ThemeData set ThemeName=' + QuotedStr(sName)
        + ',Duration=' + QuotedStr(IntToStr(iDuration))
        + ' where ThemeID=' + QuotedStr(IntToStr(FSelectID));

  if ExecSql(sSql) then
  begin
    SendMessage(FParentHandle, WM_REFRESHDATA, 0, 0);

    MsgBoxDefault(RESSAVESUCCESS);
  end
  else
    MsgBoxDefault(RESSAVEFAIL);

end;

procedure TfrmThemeEdit.FormShow(Sender: TObject);
var
  sSql: string;
begin
  inherited;

  sSql := 'select * from ThemeData where ThemeID=''' + IntToStr(FSelectID) + '''';

  with qryTmp do
  begin
    close;
    SQL.Text := sSql;
    Open;

    edtName.Text := Trim(FieldByName('ThemeName').AsString);
    edtDuration.Value := FieldByName('Duration').AsInteger;
  end;
end;

end.
